#include "projectmodelitem.h"

#include "projectitem.h"

class ProjectModelItemPrivate{
	public:
		ProjectModelItemPrivate() {}
		~ProjectModelItemPrivate() {}
		 ProjectItem* rootItem;
};

ProjectModelItem::ProjectModelItem(ProjectItem* item, QObject *parent) :
	QAbstractItemModel(parent), O_PRIVATE_CREATE(ProjectModelItem)
{
	O_D(ProjectModelItem);
	d->rootItem = item;
}

ProjectModelItem::~ProjectModelItem()
{
	O_PRIVATE_DELETE;
}

int ProjectModelItem::columnCount(const QModelIndex &parent) const
{
   Q_UNUSED(parent);
   return 1;
}	

Qt::ItemFlags ProjectModelItem::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}


QVariant ProjectModelItem::data(const QModelIndex &index, int role) const
{
    O_C(ProjectModelItem);
    if (!index.isValid())
        return QVariant();

    if (role != Qt::DisplayRole)
        return QVariant();

    ProjectItem *item = static_cast<ProjectItem*>(index.internalPointer());

    //return item->data(index.column());
    return item->name();
}


QVariant ProjectModelItem::headerData(int section, Qt::Orientation orientation, int role) const
 {
    O_C(ProjectModelItem);
     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
         return d->rootItem->name();

     return QVariant();
 }

 QModelIndex ProjectModelItem::index(int row, int column, const QModelIndex &parent)const
 {
     O_C(ProjectModelItem);
     if (!hasIndex(row, column, parent))
         return QModelIndex();

     ProjectItem *parentItem;

     if (!parent.isValid())
         parentItem = d->rootItem;
     else
         parentItem = static_cast<ProjectItem*>(parent.internalPointer());

     ProjectItem *childItem = parentItem->items().at(row);
     if (childItem)
         return createIndex(row, column, childItem);
     else
         return QModelIndex();
 }

 QModelIndex ProjectModelItem::parent(const QModelIndex &index) const
 {
     O_C(ProjectModelItem);
     if (!index.isValid())
         return QModelIndex();

     ProjectItem *childItem = static_cast<ProjectItem*>(index.internalPointer());
     ProjectItem *parentItem = (ProjectItem*)childItem->parent();

     if (parentItem == d->rootItem)
         return QModelIndex();

     ProjectItem* parent2 = (ProjectItem*)parentItem->parent();
     int row = parent2->items().indexOf(parentItem);
     return createIndex(row, 0, parentItem);
 }

 int ProjectModelItem::rowCount(const QModelIndex &parent) const
 {
     O_C(ProjectModelItem);
     ProjectItem *parentItem;
     if (parent.column() > 0)
         return 0;

     if (!parent.isValid())
         parentItem = d->rootItem;
     else
         parentItem = static_cast<ProjectItem*>(parent.internalPointer());

     return parentItem->items().count();
 }
